6  UK Postcode Maps

Author

Chrissy h Roberts

6.1 Background

This is a basic tutorial on how to load shapefiles and geopoints to a leaflet map.

There is a lot more detailed information here [https://rstudio.github.io/leaflet/](https://rstudio.github.io/leaflet/)

The example is hopefully useful as it uses UK postcode and district shapefiles, which are commonly useful for a variety of purposes.

UK Postcode data can be found here

https://www.freemaptools.com/map-tools.htm

UK district shapefiles are available here

https://www.ordnancesurvey.co.uk/opendatadownload/products.html

6.2 Libraries

library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.4.1     ✔ purrr   1.0.1
✔ tibble  3.2.1     ✔ dplyr   1.1.0
✔ tidyr   1.3.0     ✔ stringr 1.5.0
✔ readr   2.1.4     ✔ forcats 1.0.0
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
library(sf)
Linking to GEOS 3.10.2, GDAL 3.4.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(knitr)
library(leaflet)

6.3 Data

6.3.1 Create a folder to house data

if(!dir.exists("data/ukpostcodes"))(dir.create("data/ukpostcodes"))
[1] TRUE
system("rm -rf data/ukpostcodes/*")

6.4 Download a list of UK postcodes and gps locations

download.file(url = "https://data.freemaptools.com/download/full-uk-postcodes/ukpostcodes.zip",destfile =paste("data/ukpostcodes/ukpostcodes.zip"))

system ("unzip data/ukpostcodes/ukpostcodes.zip -d data/ukpostcodes/")

6.5 Download a list of UK district shapefiles

# set a timeout proportional to the size of the dataset and the speed of the connection
options(timeout=300)

download.file(url = "https://api.os.uk/downloads/v1/products/BoundaryLine/downloads?area=GB&format=ESRI%C2%AE+Shapefile&redirect",destfile =paste("data/ukpostcodes/bdline_essh_gb.zip"))

system ("unzip data/ukpostcodes/bdline_essh_gb.zip -d data/ukpostcodes/")

6.6 Read Postcode data

ukpostcodes <- read_csv("data/ukpostcodes/ukpostcodes.csv")
Rows: 1792783 Columns: 4
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (1): postcode
dbl (3): id, latitude, longitude

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

6.7 Make a small sample of all postcodes

sample<-ukpostcodes[sample(1:nrow(ukpostcodes),size = 100),] %>% 
  mutate(
    latitude = as.numeric(latitude),
    longitude = as.numeric(longitude)
  )
head(sample)
# A tibble: 6 × 4
       id postcode latitude longitude
    <dbl> <chr>       <dbl>     <dbl>
1  843728 M14 6YY      53.4    -2.21 
2  841201 M19 2EF      53.4    -2.20 
3  981297 KT15 2NA     51.4    -0.488
4 1570712 AL8 6HT      51.8    -0.215
5 1701989 BT19 6XB     54.6    -5.62 
6  826078 M34 6BW      53.5    -2.10 

6.8 Read in shapefiles to a map

map = read_sf("data/ukpostcodes/Data/GB/county_region.dbf")
mapproj <- st_transform(map, st_crs("+proj=longlat +init=epsg:4326 +ellps=WGS84 +datum=WGS84 +no_defs"))
Warning in CPL_crs_from_input(x): GDAL Message 1: +init=epsg:XXXX syntax is
deprecated. It might return a CRS with a non-EPSG compliant axis order.
leaflet(mapproj) %>% 
    addTiles() %>% 
  addMeasure(primaryLengthUnit="kilometers", secondaryLengthUnit="meters")  %>%
  addScaleBar(options = c(imperial = FALSE)) %>%  
  addPolygons(color = "green",label = mapproj$NAME) %>% 
  addCircleMarkers(lng = sample$longitude,lat = sample$latitude,label = sample$postcode,radius = 0.3)

6.8.1 remove data

system("rm -rf data/ukpostcodes/")